package com.example.demo.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.example.demo.entity.Banners;
import com.example.demo.entity.Category;
import com.example.demo.service.ICategoryService;
import com.example.demo.utils.PageResult;
import com.example.demo.utils.PassToken;
import com.example.demo.utils.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * categoryController
 * 
 * @author ruoyi
 * @date 2025-04-02
 */
@RestController
@RequestMapping("/system/category")
public class CategoryController
{
    @Autowired
    private ICategoryService categoryService;

    /**
     * 查询category列表
     */
    @PassToken
    @GetMapping("/list")
    public ResponseResult list(Category category, @RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "10")int pageSize)
    {
        PageResult pageResult=categoryService.selectCategoryList(category,pageNum,pageSize);

        return ResponseResult.createBySuccess(200,pageResult,"获取成功");
    }
    @PassToken
    @GetMapping("/list1")
    public ResponseResult list1(Category category, @RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "10")int pageSize)
    {
        PageResult pageResult=categoryService.selectCategoryGoodsList(category,pageNum,pageSize);

        return ResponseResult.createBySuccess(200,pageResult,"获取成功");
    }
    /**
     * 导出category列表
     */
//    @PreAuthorize("@ss.hasPermi('system:category:export')")
//    @Log(title = "category", businessType = BusinessType.EXPORT)
//    @PostMapping("/export")
//    public void export(HttpServletResponse response, Category category)
//    {
//        List<Category> list = categoryService.selectCategoryList(category);
//        ExcelUtil<Category> util = new ExcelUtil<Category>(Category.class);
//        util.exportExcel(response, list, "category数据");
//    }

    /**
     * 获取category详细信息
     */
//    @PreAuthorize("@ss.hasPermi('system:category:query')")
    @GetMapping(value = "/{cid}")
    public ResponseResult getInfo(@PathVariable("cid") Long cid)
    {Category category=categoryService.selectCategoryByCid(cid);
        return ResponseResult.createBySuccess(200,category,"查询成功");
    }

    /**
     * 新增category
     */
//    @PreAuthorize("@ss.hasPermi('system:category:add')")
//    @Log(title = "category", businessType = BusinessType.INSERT)
    @PostMapping
    public ResponseResult add(@RequestBody Category category){
    int i = categoryService.insertCategory(category);
        if (i > 0) {
    return ResponseResult.createBySuccess(200, "", "添加成功");
} else {
    return ResponseResult.createByFail(500, "添加失败");
}
}

    /**
     * 修改category
     */
//    @PreAuthorize("@ss.hasPermi('system:category:edit')")
//    @Log(title = "category", businessType = BusinessType.UPDATE)
    @PutMapping
    public ResponseResult edit(@RequestBody Category category)
    {
        int i = categoryService.updateCategory(category);
        if (i > 0) {
            return ResponseResult.createBySuccess(200, "", "更新成功");
        } else {
            return ResponseResult.createByFail(500, "更新失败");
        }
    }

    /**
     * 删除category
     */
//    @PreAuthorize("@ss.hasPermi('system:category:remove')")
//    @Log(title = "category", businessType = BusinessType.DELETE)
	@DeleteMapping("/{cids}")
    public ResponseResult remove(@PathVariable Long[] cids)
    {
        int i = categoryService.deleteCategoryByCids(cids);
        if (i > 0) {
            return ResponseResult.createBySuccess(200, "", "删除成功");
        } else {
            return ResponseResult.createByFail(500, "删除失败");
        }
    }
}
